<?php

/**
 * uniBoard ACP Admin Actions Logs
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_logs_admin_acts{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load additional lang files
		$appCore -> loadLanguageFile( 'acp_home');
		$appCore -> loadLanguageFile( 'acp_logs');
		
		// Draw Admin Actions?
		if ( $appCore -> _REQ( 'show') == -1 || $appCore -> _REQ( 'show') > 0)
		{
			// Show Logs
			$this -> showAuthorLogs();
		}
		else
		{
			// Show Main Page
			$this -> showMainPage();
		}
				
	}

	// ------------------------------------------------------------------------

	/**
	 * Draws Author Logs
	 *
	 */
	
	function showAuthorLogs(){
		
		// Grab Author ID
		$author_id = (integer) $this -> appCore -> _REQ( 'show');
		
		// Count Author Logs
		$author_logs = $this -> appCore -> db -> count( 'logs_admin', 'user_id = \'' . $author_id . '\'');
		
		// Cache output
		ob_start();
		
		// Found Log?
		if ( $author_logs > 0)
		{
			// Author name
			$author_name = '';
			
			// We will Display 15 logs per page
			$paginator = new class_paginator( $this -> appCore, $this -> appCore -> adminLink( 'act=logs_admin_acts&show=' . $author_id . '&st=%s'), $author_logs, 15);
			
			// Draw Paginator
			$paginator_html = $paginator -> draw();
			
			// Display Paginator
			echo $paginator_html;
		
			// Select Author Logs
			$this -> appCore -> db -> query( 'SELECT l.log_id, l.user_id, l.user_name, l.log_ip, l.log_date, l.log_content, l.log_target , u.user_id, u.user_name, g.group_prefix, g.group_suffix
			FROM ' . DB_PREFIX . 'logs_admin l
			LEFT JOIN ' . DB_PREFIX . 'users u ON l.user_id = u.user_id
			LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
			WHERE l.user_id = \'' . $author_id . '\'
			ORDER BY l.log_date DESC
			LIMIT ' . $paginator -> getPosition() . ',15');
		
			// Draw row with result
			while ( $admin_result = $this -> appCore -> db -> fetch_array())
			{
				// Set Author?
				if ( !isset( $author_name[0]))
				{
					// Set Author name
					$author_name = ( $author_id > 0 ? htmlspecialchars( $admin_result[8]) : $this -> appCore -> lang['logs_list_author_guest']);
					
					// Open Block
					echo $this -> appCore -> output -> openTableBlock( $this -> appCore -> lang['logs_list_by_author'] . ': ' . $author_name);
		
					// Header
					echo $this -> appCore -> output -> tableHeader( array(
						array(
							'left_float width_big',
							$this -> appCore -> lang['column_action']
						),
						array(
							'right_float width_big',
							$this -> appCore -> lang['column_date']
						),
						array(
							'right_float width_big',
							$this -> appCore -> lang['column_ip']
						)
					));
				}
				
				// Draw row
				echo $this -> appCore -> output -> tableRow( '
					<div class="cell left_float width_long">
						<b>' . $this -> appCore -> lang[$admin_result[5]] . '</b>' . ( isset( $admin_result[6][0]) ? '<div class="item_description">' . ( $admin_result[6][0] == '{' ? $this -> appCore -> ifString( $admin_result[6]) : $admin_result[6]) . '</div>' : '') . '
					</div>
					<div class="cell left_border right_float width_big center">
						' . $this -> appCore -> getDate( $admin_result[4], 'short') . '
					</div>
					<div class="cell left_border right_float width_big center">
						' . $admin_result[3] . '
					</div>');		
			}
			
			// Free result
			$this -> appCore -> db -> freeResult();
		
			// Close Block
			echo $this -> appCore -> output -> closeTableBlock();
			
			// Display Paginator
			echo $paginator_html;
			
			// Draw output
			$this -> appCore -> output -> draw( $this -> appCore -> lang['logs_list_by_author'], array( $this -> appCore -> adminLink(
				'act=logs_admin_acts') => $this -> appCore -> lang['acp_admin_actions'],
				'' => $this -> appCore -> lang['logs_list_by_author'] . ': ' . $author_name
			), ob_get_clean());
		}
		else
		{
			// Nada, Error
			$this -> appCore -> output -> error( $this -> appCore -> lang['acp_logs_not_found_author'], $this -> appCore -> lang['acp_admin_actions']);
			
			// List of Logs
			$this -> showMainPage();	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Main Page
	 *
	 */
	
	function showMainPage(){
		
		// Open Block
		echo $this -> appCore -> output -> openTableBlock( $this -> appCore -> lang['logs_list_by_author']);
		
		// Header
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float width_big',
				$this -> appCore -> lang['column_user']
			),
			array(
				'right_float width_medium',
				$this -> appCore -> lang['column_action']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['logs_list_logs']
			)
		));
		
		// Select Authors from DB
		$this -> appCore -> db -> query( 'SELECT l.user_id, u.user_name, COUNT( l.log_id) AS author_logs, g.group_prefix, g.group_suffix
		FROM ' . DB_PREFIX . 'logs_admin l
		LEFT JOIN ' . DB_PREFIX . 'users u ON l.user_id = u.user_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
		GROUP BY l.user_id');
		
		// Draw row with result
		while ( $admin_result = $this -> appCore -> db -> fetch_array())
		{
			// Draw row
			echo $this -> appCore -> output -> tableRow( '
				<div class="cell left_float width_big">
					<b>' . $admin_result[3] . ( $admin_result[0] > 0 ? htmlspecialchars( $admin_result[1]) : '<i>' . $this -> appCore -> lang['logs_list_author_guest'] . '</i>') . $admin_result[4] . '</b>
				</div>
				<div class="cell left_border right_float width_big center">
					<a href="' . $this -> appCore -> adminLink( 'act=logs_admin_acts&show=' . $admin_result[0]) . '">' . $this -> appCore -> lang['logs_list_show_logs'] . '</a>
				</div>
				<div class="cell left_border right_float width_medium center">
					' . $this -> appCore -> number( $admin_result[2]) . '
				</div>');
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
				
		// Close Block
		echo $this -> appCore -> output -> closeTableBlock();
		
		// Open Block
		echo $this -> appCore -> output -> openTableBlock( $this -> appCore -> lang['logs_last_5_logs_list']);
		
		// Header
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float width_big',
				$this -> appCore -> lang['column_user']
			),
			array(
				'right_float width_long',
				$this -> appCore -> lang['column_action']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_date']
			)
		));
		
		// Select Logs from DB
		$this -> appCore -> db -> query( 'SELECT l.log_id, l.user_id, l.user_name, l.log_ip, l.log_date, l.log_content, l.log_target , u.user_id, u.user_name, g.group_prefix, g.group_suffix
		FROM ' . DB_PREFIX . 'logs_admin l
		LEFT JOIN ' . DB_PREFIX . 'users u ON l.user_id = u.user_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
		ORDER BY l.log_date DESC
		LIMIT 5');
		
		// Draw row with result
		while ( $admin_result = $this -> appCore -> db -> fetch_array())
		{
			// Draw row
			echo $this -> appCore -> output -> tableRow( '
				<div class="cell left_float width_big">
					<b>' . $admin_result[9] . htmlspecialchars( $admin_result[1] > 0 ? $admin_result[8] : $admin_result[2]) . $admin_result[10] . '</b><div class="item_description">' . $admin_result[3] . '</div>
				</div>
				<div class="cell left_border right_float width_long">
					<b>' . $this -> appCore -> lang[$admin_result[5]] . '</b>' . ( isset( $admin_result[6][0]) ? '<div class="item_description">' . ( $admin_result[6][0] == '{' ? $this -> appCore -> ifString( $admin_result[6]) : $admin_result[6]) . '</div>' : '') . '
				</div>
				<div class="cell left_border right_float width_big center">
					' . $this -> appCore -> getDate( $admin_result[4], 'short') . '
				</div>');	
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Close Block
		echo $this -> appCore -> output -> closeTableBlock();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_admin_actions'], array( $this -> appCore -> adminLink( 'act=logs_admin_acts') => $this -> appCore -> lang['acp_admin_actions']), ob_get_clean());
		
	}
	
}